找传奇、传世资源到传世资源站!

C# 自定义tab按钮控件 示例 代码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

C# 自定义tab按钮控件 示例 代码 桌面应用界面/GUI-第1张using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace BaiduTabs{ class TabButton:Control { private Image _HoverImage; private Image _DownImage; private Image _DefaultImage; private ButtonStyleShow _ButtonStyleShow=ButtonStyleShow.Default; private Boolean _Border = true; private Boolean _BorderTop = true; private Boolean _BorderLeft = true; private Boolean _BorderRigth = true; private Boolean _BorderBottom = true; private Color _BorderColor = Color.CadetBlue; //属性 [Description("默认背景"), Category("ButtonBG")] [DefaultValue(null)] public Image DefaultImage { get { return _DefaultImage; } set { _DefaultImage = value; Invalidate(); } } [Description("进入背景"), Category("ButtonBG")] [DefaultValue(null)] public Image HoverImage { get { return _HoverImage; } set { _HoverImage = value; } } [Category("ButtonBG")] [Description("按下背景")] [DefaultValue(null)] public Image DownImage { get { return _DownImage; } set { _DownImage = value; } } [Category("显示")] [Description("样式")] [DefaultValue(ButtonStyleShow.Default)] public ButtonStyleShow ButtonStyle { get { return _ButtonStyleShow; } set { _ButtonStyleShow = value; Invalidate(); } } [Category("边框")] [Description("边框")] [DefaultValue(true)] public Boolean Border { get { return _Border; } set { _Border = value; Invalidate(); } } [Category("边框")] [Description("上边框")] [DefaultValue(true)] public Boolean BorderTop { get { return _BorderTop; } set { _BorderTop = value; Invalidate(); } } [Category("边框")] [Description("下边框")] [DefaultValue(true)] public Boolean BorderBottom { get { return _BorderBottom; } set { _BorderBottom = value; Invalidate(); } } [Category("边框")] [Description("左边框")] [DefaultValue(true)] public Boolean BorderLeft { get { return _BorderLeft; } set { _BorderLeft = value; Invalidate(); } } [Category("边框")] [Description("右边框")] [DefaultValue(true)] public Boolean BordeRigth { get { return _BorderRigth; } set { _BorderRigth = value; Invalidate(); } } [Category("边框")] [Description("边框颜色")] [DefaultValue(true)] public Color BordeColor { get { return _BorderColor; } set { _BorderColor = value; Invalidate(); } } public override string Text { get { return base.Text; } set { base.Text = value; Invalidate(); } } //方法 public TabButton() { this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.BackColor = Color.Transparent; this.Size = new Size(100, 30); } //枚举显示 public enum ButtonStyleShow { Default=0, Image=1 } private int show = 0; protected override void OnPaint(PaintEventArgs e) { Graphics cavan=e.Graphics; Size textsize = cavan.MeasureString(this.Text, this.Font).ToSize(); if (show == 0) { switch (_ButtonStyleShow) { case ButtonStyleShow.Default: cavan.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(3, 3, this.Size.Width - 6, this.Size.Height - 6)); cavan.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new PointF((this.Size.Width / 2) - (textsize.Width / 2), (this.Size.Height / 2) - (textsize.Height / 2))); if (_Border) { if (_BorderTop){cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, 0, this.Size.Width, 3)); }//上 if (_BorderLeft) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, 0, 3, this.Size.Height)); }//右 if (_BorderRigth) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(this.Size.Width - 3, 0, 3, this.Size.Height)); }//左 if (_BorderBottom) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, this.Height - 3, this.Size.Width, 3)); }//下 } break; case ButtonStyleShow.Image: if (_DefaultImage != null) { cavan.DrawImage(_DefaultImage, 0, 0, this.Size.Width, this.Size.Height); cavan.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new PointF((this.Size.Width / 2) - (textsize.Width / 2), (this.Size.Height / 2) - (textsize.Height / 2))); } break; } } else if (show == 1)//按下 { switch (_ButtonStyleShow) { case ButtonStyleShow.Default: cavan.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(4, 4, this.Size.Width - 6, this.Size.Height - 6)); cavan.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new PointF((this.Size.Width / 2) - (textsize.Width / 2), (this.Size.Height / 2) - (textsize.Height / 2))); if (_Border) { if (_BorderTop) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, 0, this.Size.Width, 4)); }//上 if (_BorderLeft) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, 0, 3, this.Size.Height)); }//右 if (_BorderRigth) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(this.Size.Width - 3, 0, 3, this.Size.Height)); }//左 if (_BorderBottom) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, this.Height - 3, this.Size.Width, 2)); }//下 } break; case ButtonStyleShow.Image: cavan.DrawImage(_DownImage, 0, 0, this.Size.Width, this.Size.Height); cavan.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new PointF((this.Size.Width / 2) - (textsize.Width / 2), (this.Size.Height / 2) - (textsize.Height / 2))); break; } } else if (show == 2)//移上 { switch (_ButtonStyleShow) { case ButtonStyleShow.Default: cavan.FillRectangle(new SolidBrush(this.BackColor), new Rectangle(3, 3, this.Size.Width - 6, this.Size.Height - 6)); cavan.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new PointF((this.Size.Width / 2) - (textsize.Width / 2), (this.Size.Height / 2) - (textsize.Height / 2))); if (_Border) { if (_BorderTop){cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, 0, this.Size.Width, 3)); }//上 if (_BorderLeft) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, 0, 3, this.Size.Height)); }//右 if (_BorderRigth) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(this.Size.Width - 3, 0, 3, this.Size.Height)); }//左 if (_BorderBottom) { cavan.FillRectangle(new SolidBrush(this._BorderColor), new Rectangle(0, this.Height - 3, this.Size.Width, 3)); }//下 } break; case ButtonStyleShow.Image: cavan.DrawImage(_HoverImage, 0, 0, this.Size.Width, this.Size.Height); cavan.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), new PointF((this.Size.Width / 2) - (textsize.Width / 2), (this.Size.Height / 2) - (textsize.Height / 2))); break; } } base.OnPaint(e); } protected override void OnMouseDown(MouseEventArgs e) { show = 1; Invalidate(); base.OnMouseDown(e); } protected override void OnMouseUp(MouseEventArgs e) { show = 0; Invalidate(); base.OnMouseUp(e); } private Color FontColor1; protected override void OnMouseEnter(EventArgs e) { FontColor1 = this.ForeColor; this.ForeColor = Color.FromKnownColor(KnownColor.Highlight); show = 2; Invalidate(); base.OnMouseEnter(e); } protected override void OnMouseLeave(EventArgs e) { this.ForeColor=FontColor1; show = 0; Invalidate(); base.OnMouseLeave(e); } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复